//
//  UIColor+TYColor.m
//  LIveDemo
//
//  Created by Thank_Hyu on 2019/3/26.
//  Copyright © 2019 cchuanfeng. All rights reserved.
//

#import "UIColor+TYColor.h"

@implementation UIColor (TYColor)
+ (UIColor *)gradientColorFromColors:(NSArray *)colors imgSize:(CGSize)imgSize {
    CGRectMake(0, 0, 0, 0);
    return [self gradientColorFromColors:colors imgSize:imgSize gradientDirection:UIColorGradientDirectionlLeftUpToRightDown];
}

+ (UIColor *)gradientColorFromColors:(NSArray *)colors imgSize:(CGSize)imgSize gradientDirection:(UIColorGradientDirection)gradientDirection{
    NSMutableArray *ar = [NSMutableArray array];
    
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    
    UIGraphicsBeginImageContextWithOptions(imgSize, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextClearRect(context, CGRectMake(0, 0, imgSize.width, imgSize.height)); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start;
    CGPoint end;
    switch (gradientDirection) {
        case UIColorGradientDirectionHorizontal:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(imgSize.width, 0);
            break;
        case UIColorGradientDirectionVertical:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(0, imgSize.height);
            break;
        case UIColorGradientDirectionlLeftUpToRightDown:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(imgSize.width, imgSize.height);
            break;
        case UIColorGradientDirectionlLeftDownToRightUp:
            start = CGPointMake(0.0, imgSize.height);
            end = CGPointMake(imgSize.width, 0.0);
            break;
        default:
            break;
    }
    
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return [UIColor colorWithPatternImage:image];
}

- (RGBA)getRGB {
    CGFloat red = 0.0;
    CGFloat green = 0.0;
    CGFloat blue = 0.0;
    CGFloat alpha = 0.0;
    [self getRed:&red green:&green blue:&blue alpha:&alpha];
    RGBA rgba = {red,green,blue,alpha};
    return rgba;
}

+ (UIColor *)colorWithRGBA:(RGBA)rgba{
    return [UIColor colorWithRed:rgba.red green:rgba.green blue:rgba.blue alpha:rgba.alpha];
}

+ (UIColor *)colorWithHex:(long)hex{
    return  [UIColor colorWithRed:(((hex & 0xFF0000) >> 16))/255.0 green:(((hex &0xFF00) >>8))/255.0 blue:((hex &0xFF))/255.0 alpha:1.0];
}

+ (UIColor *)colorWithHex:(long)hex alpha:(float)alpha{
    return  [UIColor colorWithRed:(((hex & 0xFF0000) >> 16))/255.0 green:(((hex &0xFF00) >>8))/255.0 blue:((hex &0xFF))/255.0 alpha:alpha];
}
@end
